package com.wolfden.drugserver.commands;

import java.util.ArrayList;
import java.util.Collections;

import org.bukkit.Location;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;

import com.wolfden.drugserver.Constants;
import com.wolfden.drugserver.warping.SerializableLocation;

public class WarpCommandManager implements CommandExecutor {

	@Override
	public boolean onCommand(CommandSender sender, Command cmd, String label,
			String[] args) {

		Player player = null;
		if (sender instanceof Player) {
			player = (Player) sender;
		}

		String command = cmd.getName();

		if (command.equalsIgnoreCase("listwarps")
				|| command.equalsIgnoreCase("list_warps")
				|| command.equalsIgnoreCase("lw")) {
			ArrayList<String> warpNames = Constants.warpMap.getWarpsList();
			Collections.sort(warpNames);
			if (warpNames.size()!=0) {
				sender.sendMessage("Warps: "
						+ warpNames.toString().substring(1,
								warpNames.toString().length() - 1));
			} else {
				sender.sendMessage("No warps are set.");
			}
			return true;
		}

		if (player == null) {
			sender.sendMessage("You must be a player to use this command.");
			return true;
		} else if (args.length < 1) {
			sender
					.sendMessage("You must provide the appropriate number of arguments.");
			return true;
		}
		
		if (command.equalsIgnoreCase("movewarp")
				|| command.equalsIgnoreCase("move_warp")
				|| command.equalsIgnoreCase("mvwarp")) {
			if (Constants.warpMap.getWarpsList().contains(args[0].toLowerCase())) {
				Constants.warpMap.removeWarp(args[0].toLowerCase());
				Constants.warpMap.addWarp(args[0].toLowerCase(), new SerializableLocation(player.getLocation()));
				sender.sendMessage("Warp moved.");
			} else {
				sender.sendMessage("This warp does not exist.");
			}
			return true;
		} else if (command.equalsIgnoreCase("removewarp")
				|| command.equalsIgnoreCase("remove_warp")
				|| command.equalsIgnoreCase("rmwarp")) {
			if (Constants.warpMap.removeWarp(args[0].toLowerCase())) {
				sender.sendMessage("Warp removed.");
			} else {
				sender.sendMessage("This warp does not exist.");
			}
			return true;
		} else if (command.equalsIgnoreCase("setwarp") || command.equalsIgnoreCase("set_warp")) {
			if (!Constants.warpMap.getWarpsList().contains(args[0].toLowerCase())) {
				Constants.warpMap.addWarp(args[0].toLowerCase(), new SerializableLocation(player.getLocation()));
				sender.sendMessage("Warp set.");
			} else {
				sender.sendMessage("A warp with this name already exists.");
			}
			return true;
		} else if (command.equalsIgnoreCase("warp")) {
			Location originalLocation = player.getLocation();
			if (Constants.warpMap.getWarpsList().contains(args[0].toLowerCase())) {
				if (!player
						.teleport(Constants.warpMap.getWarpLocation(args[0].toLowerCase()))) {
					player.teleport(originalLocation);
					sender.sendMessage("Warp could not be completed.");
				}
			} else {
				sender.sendMessage("This warp does not exist.");
			}
			return true;
		}

		if (args.length < 2) {
			sender
					.sendMessage("You must provide the appropriate number of arguments.");
			return true;
		}

		if (command.equalsIgnoreCase("renamewarp")
				|| command.equalsIgnoreCase("rename_warp")) {
			if (Constants.warpMap.getWarpsList().contains(args[0].toLowerCase())) {
				if (!Constants.warpMap.renameWarp(args[0].toLowerCase(), args[1].toLowerCase())) {
					sender.sendMessage("A warp with this name already exists.");
				} else {
					sender.sendMessage("Warp renamed.");
				}
			} else {
				sender
						.sendMessage("The warp you want to rename does not exist.");
			}
			return true;
		}

		return false;
	}
}